package seqlist.oj;

/**
 * @Author: yuisama
 * @Date: 2019/12/11 09:47
 * @Description:分隔链表
 * 给定一个链表和一个特定值 x，对链表进行分隔，使得所有小于 x 的节点都在大于或等于 x 的节点之前。
 * 你应当保留两个分区中每个节点的初始相对位置。
 * 输入: head = 1->4->3->2->5->2, x = 3
 * 输出: 1->2->2->4->3->5
 * https://leetcode-cn.com/problems/partition-list/
 */
public class Solution86 {
    public ListNode partition(ListNode head, int x) {
        if(head == null || head.next == null) {
            return head;
        }
        ListNode smallHead = new ListNode(-1);
        ListNode smallTemp = smallHead;
        ListNode bigHead = new ListNode(-1);
        ListNode bigTemp = bigHead;
        while (head != null) {
            if (head.val < x) {
                smallTemp.next = head;
                smallTemp = head;
            }else {
                bigTemp.next = head;
                bigTemp = head;
            }
            head = head.next;
        }
        bigTemp.next = bigTemp = null;
        smallTemp.next = bigHead.next;
        return smallHead.next;
    }
}
